package com.stylefeng.guns.rest.modular.film.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.film.FilmServiceAPI;
import com.stylefeng.guns.api.film.vo.*;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName DefaultFilmServiceImpl
 * Created by zghgchao 2019/8/6 10:56
 **/
@Component
@Service(interfaceClass = FilmServiceAPI.class)
public class DefaultFilmServiceImpl implements FilmServiceAPI {

    @Autowired
    private MoocBannerTMapper bannerTMapper;

    @Autowired
    private MoocFilmTMapper filmTMapper;

    @Autowired
    private MoocCatDictTMapper catDictTMapper;

    @Autowired
    private MoocSourceDictTMapper sourceDictTMapper;

    @Autowired
    private MoocYearDictTMapper yearDictTMapper;

    @Autowired
    private MoocFilmInfoTMapper filmInfoTMapper;

    @Autowired
    private MoocFilmActorTMapper filmActorTMapper;

    @Autowired
    private MoocActorTMapper actorTMapper;

    @Override
    public List<BannerVO> getBanners() {

        List<BannerVO> result = new ArrayList<>();

        List<MoocBannerT> moocBannerTS = bannerTMapper.selectList(null);
        for (MoocBannerT bannerT : moocBannerTS) {
            BannerVO bannerVO = new BannerVO();

            bannerVO.setBannerId(bannerT.getUuid()+"");
            bannerVO.setBannerAddress(bannerT.getBannerAddress());
            bannerVO.setBannerUrl(bannerT.getBannerUrl());

            result.add(bannerVO);
        }
        return result;
    }

    @Override
    public FilmVO getHotFilms(boolean isLimit, int nums,int nowPage,int sortId,int sourceId,int yearId,int catId) {
        FilmVO filmVO = new FilmVO();
        List<FilmInfo> filmInfos = new ArrayList<>();

        // 热映影片的限制条件
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status",1);

        // 判断是否是首页需要的内容
        if (isLimit){
            // 如果是，则限制条数、限制内容为热映影片
            Page<MoocFilmT> page = new Page<>(1,nums);
            List<MoocFilmT> moocFilmTS = filmTMapper.selectPage(page, entityWrapper);

            // 组织FilmInfo
            filmInfos = this.getFilmInfos(moocFilmTS);

            filmVO.setFilmNum(filmInfos.size());
            filmVO.setFilmInfo(filmInfos);
        }else {
            // 如果不是，则是列表页，同样需要限制内容为热映影片
            Page<MoocFilmT> page;

            // 排序方式：1-按热门搜索，2-按时间搜索，3-按评价搜索 默认为1
            switch (sortId){
                case 1:
                    page = new Page<>(nowPage,nums,"film_box_office");
                    break;
                case 2:
                    page = new Page<>(nowPage,nums,"film_time");
                    break;
                case 3:
                    page = new Page<>(nowPage,nums,"film_score");
                    break;
                default:
                    page = new Page<>(nowPage,nums,"film_box_office");
                    break;
            }

            // 如果sourceId、yearId、catId不为99，则表示按照对应的编号进行查询
            if (sourceId != 99){
                entityWrapper.eq("film_source",sortId);
            }
            if (catId != 99){
                String catStr = "%#"+ catId +"#%";
                entityWrapper.like("film_cats",catStr);
            }
            if (yearId != 99){
                entityWrapper.eq("film_date",yearId);
            }
            List<MoocFilmT> moocFilmTS = filmTMapper.selectPage(page, entityWrapper);

            // 组织FilmInfo
            filmInfos = this.getFilmInfos(moocFilmTS);
            filmVO.setFilmNum(filmInfos.size());

            // 需要总页数 totalCounts/nums -> 0 + 1 = 1
            int totalCounts = filmTMapper.selectCount(entityWrapper);
            int totalPage = (totalCounts/nums) + 1;

            filmVO.setTotalPage(totalPage);
            filmVO.setNowPage(nowPage);

            filmVO.setFilmInfo(filmInfos);
        }

        return filmVO;
    }

    @Override
    public FilmVO getSoonFilms(boolean isLimit, int nums,int nowPage,int sortId,int sourceId,int yearId,int catId) {
        FilmVO filmVO = new FilmVO();
        List<FilmInfo> filmInfos = new ArrayList<>();

        // 即将上映影片的限制条件
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status",2);

        // 判断是否是首页需要的内容
        if (isLimit){
            // 如果是，则限制条数、限制内容为热映影片
            Page<MoocFilmT> page = new Page<>(1,nums);
            List<MoocFilmT> moocFilmTS = filmTMapper.selectPage(page, entityWrapper);

            // 组织FilmInfo
            filmInfos = this.getFilmInfos(moocFilmTS);

            filmVO.setFilmNum(filmInfos.size());
            filmVO.setFilmInfo(filmInfos);
        }else {
            // 如果不是，则是列表页，同样需要限制内容为即将上映影片
            Page<MoocFilmT> page;

            // 排序方式：1-按热门搜索，2-按时间搜索，3-按评价搜索 默认为1
            switch (sortId){
                case 1:
                    page = new Page<>(nowPage,nums,"film_preSaleNum"); // 即将上映影片，没有票房记录，可以通过 预售数量进行排序
                    break;
                case 2:
                    page = new Page<>(nowPage,nums,"film_time");
                    break;
                case 3:
                    page = new Page<>(nowPage,nums,"film_preSaleNum");// 即将上映电影（未上映没有评分），使用预售量进行评价排序
                    break;
                default:
                    page = new Page<>(nowPage,nums,"film_preSaleNum");
                    break;
            }
            List<MoocFilmT> moocFilmTS = filmTMapper.selectPage(page, entityWrapper);

            // 如果sourceId、yearId、catId不为99，则表示按照对应的编号进行查询
            if (sourceId != 99){
                entityWrapper.eq("film_source",sortId);
            }
            if (catId != 99){
                String catStr = "%#"+ catId +"#%";
                entityWrapper.like("film_cats",catStr);
            }
            if (yearId != 99){
                entityWrapper.eq("film_date",yearId);
            }

            // 组织FilmInfo
            filmInfos = this.getFilmInfos(moocFilmTS);

            // 需要总页数 totalCounts/nums -> 0 + 1 = 1
            int totalCounts = filmTMapper.selectCount(entityWrapper);
            int totalPage = (totalCounts/nums) + 1;

            filmVO.setTotalPage(totalPage);
            filmVO.setNowPage(nowPage);

            filmVO.setFilmNum(filmInfos.size());
            filmVO.setFilmInfo(filmInfos);
        }
        return filmVO;
    }

    @Override
    public List<FilmInfo> getBoxRanking() {
        // 条件 -> 正在上映的，票房前10名
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status",1);

        // 默认倒序 DESC
        Page<MoocFilmT> page = new Page<>(1,10,"film_box_office");

        List<MoocFilmT> moocFilmTS = filmTMapper.selectPage(page, entityWrapper);

        // 组织FilmInfo
        List<FilmInfo> filmInfos = this.getFilmInfos(moocFilmTS);

        return filmInfos;
    }

    @Override
    public FilmVO getClassicFilms(int nums, int nowPage, int sortId, int sourceId, int yearId, int catId) {
        FilmVO filmVO = new FilmVO();
        List<FilmInfo> filmInfos = new ArrayList<>();

        // 经典影片的限制条件
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status",3);

        Page<MoocFilmT> page;

        // 排序方式：1-按热门搜索，2-按时间搜索，3-按评价搜索 默认为1
        switch (sortId){
            case 1:
                page = new Page<>(nowPage,nums,"film_box_office");
                break;
            case 2:
                page = new Page<>(nowPage,nums,"film_time");
                break;
            case 3:
                page = new Page<>(nowPage,nums,"film_score");
                break;
            default:
                page = new Page<>(nowPage,nums,"film_box_office");
                break;
        }

        List<MoocFilmT> moocFilmTS = filmTMapper.selectPage(page, entityWrapper);

        // 如果sourceId、yearId、catId不为99，则表示按照对应的编号进行查询
        if (sourceId != 99){
            entityWrapper.eq("film_source",sortId);
        }
        if (catId != 99){
            String catStr = "%#"+ catId +"#%";
            entityWrapper.like("film_cats",catStr);
        }
        if (yearId != 99){
            entityWrapper.eq("film_date",yearId);
        }

        // 组织FilmInfo
        filmInfos = this.getFilmInfos(moocFilmTS);

        // 需要总页数 totalCounts/nums -> 0 + 1 = 1
        int totalCounts = filmTMapper.selectCount(entityWrapper);
        int totalPage = (totalCounts/nums) + 1;

        filmVO.setTotalPage(totalPage);
        filmVO.setNowPage(nowPage);

        filmVO.setFilmNum(filmInfos.size());
        filmVO.setFilmInfo(filmInfos);

        return filmVO;
    }

    @Override
    public List<FilmInfo> getExpectRanking() {
        // 条件 -> 即将上映的，预售前10名
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status",2);

        // 默认倒序 DESC
        Page<MoocFilmT> page = new Page<>(1,10,"film_preSaleNum");

        List<MoocFilmT> moocFilmTS = filmTMapper.selectPage(page, entityWrapper);

        // 组织FilmInfo
        List<FilmInfo> filmInfos = this.getFilmInfos(moocFilmTS);

        return filmInfos;
    }

    @Override
    public List<FilmInfo> getTop() {
        // 条件 -> 正在上映的，评分前10名
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status",1);

        // 默认倒序 DESC
        Page<MoocFilmT> page = new Page<>(1,10,"film_score");

        List<MoocFilmT> moocFilmTS = filmTMapper.selectPage(page, entityWrapper);

        // 组织FilmInfo
        List<FilmInfo> filmInfos = this.getFilmInfos(moocFilmTS);

        return filmInfos;
    }

    @Override
    public List<CatVO> getCats() {
        List<CatVO> catVOS = new ArrayList<>();
        List<MoocCatDictT> moocCatDictTS = catDictTMapper.selectList(null);
        for (MoocCatDictT moocCatDictT : moocCatDictTS) {
            CatVO catVO = new CatVO();
            catVO.setCatId(moocCatDictT.getUuid() + "");
            catVO.setCatName(moocCatDictT.getShowName());

            catVOS.add(catVO);
        }
        return catVOS;
    }

    @Override
    public List<SourceVO> getSources() {
        List<SourceVO> sourceVOS = new ArrayList<>();
        List<MoocSourceDictT> moocSourceDictTS = sourceDictTMapper.selectList(null);
        for (MoocSourceDictT moocSourceDictT : moocSourceDictTS) {
            SourceVO sourceVO = new SourceVO();
            sourceVO.setSourceId(moocSourceDictT.getUuid() + "");
            sourceVO.setSourceName(moocSourceDictT.getShowName());

            sourceVOS.add(sourceVO);
        }
        return sourceVOS;
    }

    @Override
    public List<YearVO> getYears() {
        List<YearVO> yearVOS = new ArrayList<>();
        List<MoocYearDictT> moocYearDictTS = yearDictTMapper.selectList(null);
        for (MoocYearDictT moocYearDictT : moocYearDictTS) {
            YearVO yearVO = new YearVO();
            yearVO.setYearId(moocYearDictT.getUuid() + "");
            yearVO.setYearName(moocYearDictT.getShowName());

            yearVOS.add(yearVO);
        }
        return yearVOS;
    }

    /**
     *
     * @param searchType  1-按名称查询，2-按uuid查
     * @param searchParam
     * @return
     */
    @Override
    public FilmDetailVO getFilmDetail(int searchType, String searchParam) {
        FilmDetailVO filmDetailVO;
        if (searchType == 1){
            filmDetailVO = filmTMapper.getFilmDetailByName(searchParam);
        }else {
            filmDetailVO = filmTMapper.getFilmDetailById(searchParam);
        }
        return filmDetailVO;
    }


    @Override
    public FilmDescVO getFilmDesc(String filmId) {

        MoocFilmInfoT filmInfo = this.getFilmInfo(filmId);

        FilmDescVO filmDescVO = new FilmDescVO();
        filmDescVO.setBiography(filmInfo.getBiography());
        filmDescVO.setFilmId(filmId);

        return filmDescVO;
    }

    @Override
    public ImgVO getImgs(String filmId) {
        MoocFilmInfoT filmInfo = this.getFilmInfo(filmId);

        // 图片地址是五个以 逗号 为分割的链接
        String filmImgStr = filmInfo.getFilmImgs();
        String[] filmImgs = filmImgStr.split(",");

        ImgVO imgVO = new ImgVO();
        imgVO.setMainImg(filmImgs[0]);
        imgVO.setImg01(filmImgs[1]);
        imgVO.setImg02(filmImgs[2]);
        imgVO.setImg03(filmImgs[3]);
        imgVO.setImg04(filmImgs[4]);

        return imgVO;
    }

    @Override
    public ActorVO getDectInfo(String filmId) {
        MoocFilmInfoT filmInfo = this.getFilmInfo(filmId);

        // 获取导演编号
        Integer directorId = filmInfo.getDirectorId();

        MoocActorT moocActorT = actorTMapper.selectById(directorId);

        ActorVO actorVO = new ActorVO();
        actorVO.setImgAddress(moocActorT.getActorImg());
        actorVO.setDirectorName(moocActorT.getActorName());

        return actorVO;
    }

    @Override
    public List<ActorVO> getActors(String filmId) {
        List<ActorVO> actors = actorTMapper.getActors(filmId);
        return actors;
    }

    private MoocFilmInfoT getFilmInfo(String filmId){
        MoocFilmInfoT moocFilmInfoT = new MoocFilmInfoT();
        moocFilmInfoT.setFilmId(filmId);

        moocFilmInfoT = filmInfoTMapper.selectOne(moocFilmInfoT);

        return moocFilmInfoT;
    }

    private List<FilmInfo> getFilmInfos(List<MoocFilmT> moocFilmTS){
        List<FilmInfo> filmInfos = new ArrayList<>();
        for (MoocFilmT moocFilmT : moocFilmTS) {
            FilmInfo filmInfo = new FilmInfo();
            filmInfo.setScore(moocFilmT.getFilmScore());
            filmInfo.setImgAdress(moocFilmT.getImgAddress());
            filmInfo.setFilmType(moocFilmT.getFilmType());
            filmInfo.setFilmScore(moocFilmT.getFilmScore());
            filmInfo.setFilmName(moocFilmT.getFilmName());
            filmInfo.setFilmId(moocFilmT.getUuid() + "");
            filmInfo.setExpectNum(moocFilmT.getFilmPresalenum());
            filmInfo.setBoxNum(moocFilmT.getFilmBoxOffice());
            filmInfo.setShowTime(DateUtil.getDay(moocFilmT.getFilmTime()));

            filmInfos.add(filmInfo);
        }

        return filmInfos;
    }
}
